package com.Setting.Bug.Service;
import com.Setting.Bug.Mapper.BugMapper;
import com.Setting.Bug.Entity.Bug;
import com.User.entity.User;
import com.User.mapper.UserMapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.utils.APIRespones;
import com.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BugService {

    @Autowired
    private BugMapper bugMapper;
    @Autowired
    private UserMapper userMapper;
    @Transactional
    public APIRespones commitBug(String token, MultipartFile[] files, String summary, String severity,
                                 String module, String regression, String description) {
        try {
            Integer userId = Utils.getTokenUserId(token);

            Bug bug = new Bug();
            bug.setSubmitterId(userId);
            bug.setSummary(summary);
            bug.setSeverity(severity);
            bug.setModule(module);
            bug.setRegression(regression);
            bug.setDescription(description);

            if (files != null && files.length > 0) {
                for (MultipartFile file : files) {
                    String imageUrl = Utils.uploadImage(file);
                    if (bug.getScreenshotPath1() == null) {
                        bug.setScreenshotPath1(imageUrl);
                    } else {
                        bug.setScreenshotPath2(imageUrl);
                    }
                    System.out.println(imageUrl);
                    System.out.println(file);
                }
            }

            System.out.println(bug.toString());
            bugMapper.insert(bug);

            Map<String, Object> responseData = new HashMap<>();
            responseData.put("message", "Bug提交成功");
            responseData.put("bugId", bug.getId());

            return APIRespones.success(responseData);
        } catch (IOException e) {
            e.printStackTrace();
            return APIRespones.failure("图片上传失败");
        } catch (Exception e) {
            e.printStackTrace();
            return APIRespones.failure("Bug提交失败");
        }
    }

    @Transactional
    public List<Bug> getProcessedBugList() {
        List<Bug> bugs = bugMapper.selectList(null);

        for (Bug bug : bugs) {
            // 获取用户信息并设置昵称
            User user = userMapper.selectById(bug.getSubmitterId());
            String displayName = (user.getNickname() != null && !user.getNickname().isEmpty()) ? user.getNickname() : user.getUsername();
            bug.setSubmitterName(displayName);

            // 下载截图文件并设置字节数组
            if (StringUtils.isNotBlank(bug.getScreenshotPath1())) {
                System.out.println("bug.getScreenshotPath1():" + bug.getScreenshotPath1());
                String screenshot1 = Utils.downloadImage(bug.getScreenshotPath1());
                bug.setScreenshot1(screenshot1);
            }

            if (StringUtils.isNotBlank(bug.getScreenshotPath2())) {
                System.out.println("bug.getScreenshotPath2():" + bug.getScreenshotPath2());
                String screenshot2 = Utils.downloadImage(bug.getScreenshotPath2());
                bug.setScreenshot2(screenshot2);
            }

        }

        return bugs;
    }


}
